<?php
namespace app\controller\model;

use app\BaseController;
use think\facade\Db;
class Classes extends BaseController
{
    public $pathID=0;
    public $tables;
    public function __construct($pathID=0){
        $this->pathID = $pathID ;
        $this->tables=new Tables();
    }
    public function rPayCompanyToOption($selectID=0)
    {
        $res = DB::name($this->tables->company)->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['company']."</option>";
        }
        return [$htmls,$arr];
    }

	public function rMyGasToOption($selectID=0,$inIDS='')
	{
		if($inIDS==''){
			return ['',[],[]];
		}
		$res = DB::name($this->tables->gas)->whereIn("id",$inIDS)->select();
		$htmls='';
		$arr=[];
		foreach ($res as $k=>$v){
			$selected='';
			if($selectID == $v['id']){
				$selected='selected';
				$arr = $v;
			}
			$htmls .="<option ".$selected." value='".$v['id']."'>".$v['name']."</option>";
		}
		return [$htmls,$arr,$res->toArray()];
	}
	public function rTipsToOption($selectID=0,$inIDS='',$cid=1)
	{
		if($inIDS!=''){
			$res = DB::name($this->tables->tips)->whereIn("id",$inIDS)->where("cid",$cid)->where("status",1)->select();
		}else{
			$res = DB::name($this->tables->tips)->where("status",1)->where("cid",$cid)->select();
		}
		$htmls='';
		$arr=[];
		$arr2=[];
		foreach ($res as $k=>$v){
			$selected='';
			$arr2[]=['title'=>$v['title'],'value'=>$v['id']];
			if($selectID == $v['id']){
				$selected='selected';
				$arr = $v;
			}
			$htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
		}
		return [$htmls,$arr,$res->toArray(),$arr2];
	}
	public function rMyServiceToOption($selectID=0,$inIDS='')
	{
		if($inIDS==''){
			return ['',[],[]];
		}
		$res = DB::name($this->tables->service)->whereIn("id",$inIDS)->select();
		$htmls='';
		$arr=[];
		foreach ($res as $k=>$v){
			$selected='';
			if($selectID == $v['id']){
				$selected='selected';
				$arr = $v;
			}
			$htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
		}
		return [$htmls,$arr,$res->toArray()];
	}
	public function rAllServiceToOption($selectID=0)
	{
		$res = DB::name($this->tables->service)->where("status",0)->select();
		$htmls='';
		$arr=[];
		foreach ($res as $k=>$v){
			$selected='';
			if($selectID == $v['id']){
				$selected='selected';
				$arr = $v;
			}
			$htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
		}
		return [$htmls,$arr,$res->toArray()];
	}
    public function rMySupplierToOption($selectID=0,$inIDS='')
    {
		if($inIDS=='')return ['',[],[]];
        $res = DB::name($this->tables->supplier)->whereIn("id",$inIDS)->where("status","")->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return [$htmls,$arr,$res->toArray()];
    }
    public function rSupplierToOption($selectID=0)
    {
        $res = DB::name($this->tables->supplier)->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return [$htmls,$arr,$res->toArray()];
    }

    public function rAuctionToOption($selectID=0)
    {
        $res = DB::name($this->tables->auction)->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return [$htmls,$arr];
    }
    public function rAuthGroupToOption($groupID,$adminID=0){//获取有特定分组权限的用户
        $res = DB::name($this->tables->authGroupAccess)->where("group_id",$groupID)->select();
        $ids = [] ;
        foreach ($res as $v){
            $ids[] = $v['uid'];
        }
        if(count($ids)<1){return ['',null];}

        $res = DB::name($this->tables->admin)->whereIn("id",$ids)->where("status",1)->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($adminID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['fullname']."</option>";
        }
        return [$htmls,$arr,$ids];
    }

    /**
     * @param $selectID 选中ID
     * @param $myID 负责人ID
     * @param $inIDS 指定IDS
     * @return array 【option，html代码 | 所有数组】
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function rWorkgroupToOption($selectID=0,$myID=0,$inIDS=[]){
        $res = DB::name($this->tables->workgroup)->where("status",0);
        if($myID>0){
            $res=$res->where("adminID",$myID);
        }
        if(count($inIDS)>0){
            $res=$res->whereIn("id",$inIDS);
        }
        $res=$res->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return [$htmls,$arr];
    }
    public function rMyGroups($groupIDS,$selectID=0){
        $res = DB::name($this->tables->makeGroup)->whereIn("id",$groupIDS)->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return [$htmls,$arr];
    }
    public function rGroupAdmin($res=[],$id=0,$selectID=0){
        if(is_array($res) && count($res)>0){
        }else{
            $res = DB::name($this->tables->makeGroup)->where("id",$id)->find();
        }
        if(!$res){return false;}
        $users = r_admin_all("id in (".implode(",",json_decode($res['userIDS'],true)).")");
        $htmls='';
        $arr=[];
        foreach ($users as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['fullname']."</option>";
        }
        return [$htmls,$arr];
    }
    public function rTableToOption($tables='',$selectID=0){
        $res = DB::name($tables)->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            if($selectID==0){
                if($k==0){
                    $selected='selected';
                    $arr = $v;
                }
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return [$htmls,$arr];
    }
    /**读取物料版本
     * @param $cint
     * @param $selectID
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function rMakeUpToOption($cint=0,$selectID=0){
        $cint>0?$this->pathID=$cint:'';
        $res = DB::name((new Tables())->makeUp)->where("proid",$this->pathID)->where("status",0)->order("id desc")->select();
        $htmls='';
        $arr=[];
        foreach ($res as $k=>$v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
                $arr = $v;
            }
            if($selectID==0){
                if($k==0){
                    $selected='selected';
                    $arr = $v;
                }
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title'].'('.$v['version'].')'."</option>";
        }
        return [$htmls,$arr];
    }
    public function rClassessToOption($cint=0,$selectID=0){
        $cint>0?$this->pathID=$cint:'';
        $res = r_classes($cint);
        $htmls='';
        foreach ($res as $v){
            $selected='';
            if($selectID == $v['id']){
                $selected='selected';
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return $htmls;
    }
    public function rAdminToOption($cint=0){
        $res = r_users();
        $htmls='';
        foreach ($res as $v){
            $selected='';
            if($cint == $v['id']){
                $selected='selected';
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['fullname']."</option>";
        }
        return $htmls;
    }
    public function rAdminGroupToOption($cint=0){
        $res = getUserGroup();
        $htmls='';
        foreach ($res as $v){
            $selected='';
            if($cint == $v['id']){
                $selected='selected';
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['title']."</option>";
        }
        return $htmls;
    }
    public function rCustomContacts($customID=0,$cint=0){
        $res =DB::name("custom_contacts")->field("id,username")->where("customID",$customID)->select();
        $htmls='';
        foreach ($res as $v){
            $selected='';
            if($cint == $v['id']){
                $selected='selected';
            }
            $htmls .="<option ".$selected." value='".$v['id']."'>".$v['username']."</option>";
        }
        return $htmls;
    }

    /**
     *       $hangyeID = 12;//客户行业
     *       $collectionID = 36;//付款方式
     *       $cityID = 43;//城市
     *       $billID = 51;//发票类型
     *       $percentID = 52;//税率
     *       $contractStatus = 59;//合同状态
     *       $sellID = 60;//销售类型
     *       $toCompanyID = 61;//归属公司
     *       $payID = 62;//付款条件
     *       $carID = 78;//运输方式
     *       $payPalID = 81;//付款平台
     *       $unit = 94;//单位
     *       $unit2 = 95;//辅助单位
     *       $materialID = 103;//产品材质
     * @param $arr
     * @param $re
     * @return array
     */
    public function rClassOption($arr=[],$re=[],$option=false){

        $reArr=[];
        foreach ($arr as $v){
            $class = r_one("classes","id=".$v);
            if($option){
                $reArr[$class['rule']] = $this->rClassessToOption($v,$re && isset($re[$class['rule']])?$re[$class['rule']]:'');
            }else{
                $reArr[$class['rule']] = r_classes($v);
            }
        }
        return $reArr;
    }
    public function rClass($id=0){
        $re = r_one("classes","id=$id");
        if(!$re){return false;}
        return $re;
    }
}
?>